package com.fr.report.script.function;

import com.fr.report.script.Function;
import com.fr.report.script.Function.Type;
import com.fr.report.script.NormalFunction;
import com.fr.report.script.Primitive;

public class SUBSTITUTE extends NormalFunction
{
  public Object run(Object[] paramArrayOfObject)
  {
    if (paramArrayOfObject.length < 3)
      return Primitive.ERROR_NAME;
    Object localObject1 = paramArrayOfObject[0];
    Object localObject2 = paramArrayOfObject[1];
    Object localObject3 = paramArrayOfObject[2];
    int i = -1;
    if (paramArrayOfObject.length > 3)
    {
      localObject4 = paramArrayOfObject[3];
      if (localObject4 instanceof Number)
        i = Math.abs(((Number)localObject4).intValue());
    }
    Object localObject4 = "" + localObject1;
    if (i < 0)
      return ((String)localObject4).replaceAll("\\Q" + localObject2 + "\\E", "" + localObject3);
    if (i > 0)
    {
      int k = -1;
      for (int j = 0; j < i; ++j)
        k = ((String)localObject4).indexOf(localObject2.toString(), k + 1);
      String str1 = ((String)localObject4).substring(0, k);
      String str2 = ((String)localObject4).substring(k, ((String)localObject4).length());
      str2 = str2.replaceFirst("\\Q" + localObject2 + "\\E", localObject3.toString());
      localObject4 = str1.concat(str2);
    }
    return localObject4;
  }

  public Function.Type getType()
  {
    return Function.TEXT;
  }

  public String getCN()
  {
    return "SUBSTITUTE(text,old_text,new_text,instance_num): 用new_text替换文本串中的old_text。\nText:需要被替换字符的文本，或含有文本的单元格引用。\nOld_text:需要被替换的部分文本。\nNew_text:用于替换old_text的文本。\nInstance_num:指定用new_text来替换第几次出现的old_text。如果指定了instance_num，则只有指定位置上的old_text被替换，否则文字串中出现的所有old_text都被new_text替换。\n备注:\n    如果需要替换文本串中的指定文本，则使用SUBSTITUTE函数；如果需要替换文本串中指定位置上的任意文本，则使用REPLACE函数。\n示例:\nSUBSTITUTE(\"data base\",\"base\",\"model\")等于“data model”。\nSUBSTITUTE(\"July 28, 2000\",\"2\",\"1\",1)等于“July 18, 2000”。\nSUBSTITUTE(\"July 28, 2000\",\"2\",\"1\")等于“July 18, 1000”。\nSUBSTITUTE(\"July 28, 2000\",\"2\",\"1\",2)等于“July 28, 1000”。 ";
  }

  public String getEN()
  {
    return "SUBSTITUTE(text,old_text,new_text,instance_num): Substitutes new_text for old_text in a text string.\nText is the text or the reference to a cell containing text for which you want to substitute characters.\nOld_text is the text you want to replace.\nNew_text is the text you want to replace old_text with.\nInstance_num specifies which occurrence of old_text you want to replace with new_text. If you specify instance_num, only that instance of old_text is replaced. Otherwise, every occurrence of old_text in text is changed to new_text.\n\nRemarks:\n    Use SUBSTITUTE when you want to replace specific text in a text string; use REPLACE when you want to replace any text that occurs in a specific location in a text string.\n\nExample:\n   SUBSTITUTE(\"data base\",\"base\",\"model\") = \"data model\"\n   SUBSTITUTE(\"July 28, 2000\",\"2\",\"1\",1) = \"July 18, 2000\"\n   SUBSTITUTE(\"July 28, 2000\",\"2\",\"1\") = \"July 18, 1000\"\n   SUBSTITUTE(\"July 28, 2000\",\"2\",\"1\",2) = \"July 28, 1000\"";
  }
}